//
//  YMTabBarViewController.m
//  WeiBo
//
//  Created by youmy on 15/8/14.
//  Copyright (c) 2015年 youmy. All rights reserved.

//  工具栏控制器

#import "YMTabBarViewController.h"
#import "YMNavigationController.h"

@interface YMTabBarViewController()<UITabBarControllerDelegate>

@property (nonatomic, strong) UIWindow *window;

@end

@implementation YMTabBarViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self config];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:YES];
}


- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    
}

- (void)config{
    
    self.view.backgroundColor = [UIColor whiteColor];
    // 设置文字的样式
    
    UITabBarItem * appearance = [UITabBarItem appearance];
    
    NSMutableDictionary * textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = RGB(123, 123, 123);
    NSMutableDictionary * selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = NAV_BAR_COLOR;
    [appearance setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [appearance setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}

/*
 *  添加一个子控制器
 *
 *  @param childVc       子控制器
 *  @param title         标题
 *  @param image         图片
 *  @param selectedImage 选中的图片
 */
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 设置子控制器标题
    childVc.title = title; // 同时设置navigationBar和tabBar的文字
    
    // 设置图片
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 设置文字的样式
    NSMutableDictionary * textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = TABBAR_COLOR;
    NSMutableDictionary * selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = TABBAR_COLOR_HIGHLIGHTED;
    
    [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    
    // 设置不对图片进行蓝色的渲染
    [childVc.tabBarItem setImage:[childVc.tabBarItem.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [childVc.tabBarItem setSelectedImage:[childVc.tabBarItem.selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    // 先给外面传来的小控制器包装一个导航控制器
    YMNavigationController * nav = [[YMNavigationController alloc] initWithRootViewController:childVc];
    // 添加子控制器
    [self addChildViewController:nav];
    
}

@end
